//
//  ZZQMainViewController.m
//  zzqKanBoKe
//
//  Created by ben on 17/2/5.
//  Copyright © 2017年 张增强. All rights reserved.
//

#import "ZZQMainViewController.h"
#import "ZZQMainTopView.h"

@interface ZZQMainViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
@property (nonatomic, strong) NSArray * dataList;
@property (nonatomic, strong)ZZQMainTopView * topView;

@end

@implementation ZZQMainViewController

- (NSArray *)dataList{
    if(!_dataList){
        _dataList = @[@"关注", @"热门", @"附近"];
    }
    return _dataList;
}

- (ZZQMainTopView *)topView{
    if(!_topView){
        _topView = [[ZZQMainTopView alloc] initWithFrame:CGRectMake(0, 0, 200, 50) titleNames:self.dataList];
        @weakify(self);
        _topView.block = ^(NSInteger tag){
            @strongify(self);
            [self.contentScrollView setContentOffset:CGPointMake(SCREEN_WIDTH*(tag-ZZQTopTypeFocue), 0) animated:YES];
        };
    }
    return _topView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initUI];
}

- (void)initUI{
    //添加左右按钮
    [self setupNav];
    //添加子视图控制器
    [self setupChildViewControllers];
}
- (void)setupChildViewControllers{
    NSArray * vcNames = @[@"ZZQFocueViewController", @"ZZQHotViewController", @"ZZQNearViewController"];
    for(NSInteger i = 0; i < vcNames.count; i++){
        NSString * vcName = vcNames[i];
        UIViewController * vc = [[NSClassFromString(vcName) alloc] init];
        vc.title = self.dataList[i];
        [self addChildViewController:vc];
    }
    
    //将子控制器的view，加到MainVC的scrollview上
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置scrollview的contentsize
    self.contentScrollView.frame = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64-49);
    self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*self.dataList.count, 0);
    self.contentScrollView.delegate = self;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.pagingEnabled = YES;
    
    //设置主控制器加载第一个页面
    [self.contentScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
//    [self scrollViewDidEndDecelerating:self.contentScrollView];
}
- (void)setupNav{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"global_search"] style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"title_button_more"] style:UIBarButtonItemStyleDone target:nil action:nil];
    
    self.navigationItem.titleView = self.topView;
    
}

#pragma mark ======
#pragma mark ====== 代理
//减速结束调用加载子控制器
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self changeScrollViewOffset:scrollView];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self changeScrollViewOffset:scrollView];
}

//封装改变scrollview偏移量
- (void)changeScrollViewOffset:(UIScrollView *)scrollView{
    CGFloat width = SCREEN_WIDTH;
    CGFloat height = SCREEN_HEIGHT;
    CGFloat offset = scrollView.contentOffset.x;
    //获取索引值
    NSInteger idx = offset / width;
    //根据索引值获取viewcontroller
    UIViewController * vc = self.childViewControllers[idx];
    //判断当前vc是否执行过viewdidload
    if(![vc isViewLoaded]){
        vc.view.frame = CGRectMake(offset, 0, width, height-64-49);
        [scrollView addSubview:vc.view];
    }
    [self.topView scrolling:idx];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
